#include "mapbubble.h"

MapBubble* 	MapBubble::instance = NULL;

MapBubble& MapBubble::GetInstance()
	{
    if (instance == NULL)
    {
      instance = new MapBubble;
    }
    
    return *instance;
	}

MapBubble::~MapBubble()
	{
	otherBubbles.ResetAndDestroy();
	otherBubbles.Close();
	
	
	}

MapBubble::MapBubble()
	: adjustable(EFalse)
	, isOtherBubblesOnTop(EFalse)
	, isPressed(EFalse)
	, isVisible(EFalse)
	, user(NULL)
	{
	
	}

BubbleUser* MapBubble::GetUser() const
	{
	return user;
	}

TBool MapBubble::IsVisible() const
	{
	return isVisible;
	}

TBool MapBubble::OnDoubleTapMap(const MotionEvent& paramMotionEvent)
	{
    if (NULL == bound){ return EFalse;}
    float postX = paramMotionEvent.GetX();
    float postY = paramMotionEvent.GetY();
    if (isVisible() && bound.Contains(postX,postY))
    {   
        isPressed = true;
        return true;
    }
    return false;
	
	}

TBool MapBubble::OnFlingMap(MotionEvent paramMotionEvent1, MotionEvent paramMotionEvent2,
        float paramFloat1, float paramFloat2)
	{
	return EFalse;
	}

TBool MapBubble::OnLongPressMap(const MotionEvent& paramMotionEvent)
	{
    if (NULL == bound){ return false;}
    float postX = paramMotionEvent.GetX();
    float postY = paramMotionEvent.GetY();
    if (isVisible() && bound.Contains(postX,postY) && isPressed)
    {   
        return true;
    }
    return false;
	}

TBool MapBubble::OnOriginalTouchEvent(const MotionEvent& event)
	{
	return false;
	}

TBool MapBubble::OnScrollMap(const MotionEvent& event1,const MotionEvent& event2, float paramFloat1, float paramFloat2)
	{
    if (isVisible() && isPressed){
        return true;
    }
    return false;
	}

TBool MapBubble::OnSingleTapMap(const MotionEvent& event)
	{
	return false;
	}

TBool MapBubble::OnSingleTapMapUnhandled(const MotionEvent& event)
	{
	return false;
	}

TBool MapBubble::OnTouchMap(const MotionEvent& paramMotionEvent)
	{
    if (NULL == bound){ return false;}
    float postX = paramMotionEvent.GetX();
    float postY = paramMotionEvent.GetY();
    if (isVisible() && bound.Contains(postX,postY) )
    {   
        isPressed = true;
        return true;
    }
    return false;
	}

TBool MapBubble::OnUpFromMap(const MotionEvent& paramMotionEvent)
	{
    if (NULL == bound){ return false;}
    float postX = paramMotionEvent.GetX();
    float postY = paramMotionEvent.GetY();
    isPressed = false;
    if (isVisible() && bound.Contains(postX,postY))
    {
        user->OnClickBubble();
        return true;
    }
    return false;
	}

// MapListener
void MapBubble::OnMapDrawn(CGraphicsContext& paramCanvas)
	{
    if (isVisible() && (NULL != user))
    {
        bound =user->DrawBubble(paramCanvas, isPressed);
    }
	}

void MapBubble::OnMapMoved(int paramInt1, int paramInt2)
	{
	
	}

void MapBubble::OnMapZoomChange(int zm, int nzm)
	{
	
	}

void MapBubble::RegisterOtherBubble(CustomBubble& customBubble)
	{
    TBool bool1 = otherBubbles.Find(&customBubble) != KErrNotFound;
    if (!bool1)
    {
    	otherBubbles.Append(&customBubble);
    }
	}

void MapBubble::Reset(BubbleUser& bubbleUser, TBool flag)
	{
	adjustable = true;
	SetVisible(flag);
	if (user != NULL 
			&& bubbleUser.Equals(*user))
		{
		return;
		}
	
	user = &bubbleUser;
  
	if (!flag)
		{
		return;
		}
	
	isOtherBubblesOnTop = false; 
	}

void MapBubble::SetOtherBubblesOnTop(TBool flag)
	{
	isOtherBubblesOnTop = flag;
	}

void MapBubble::SetVisible(TBool flag)
	{
	isVisible = flag;
	}

void MapBubble::UnregisterOtherBubble(CustomBubble& customBubble)
	{
	TInt idx;
    TBool bool1 = (idx = otherBubbles.Find(&customBubble)) != KErrNotFound;
    if (bool1)
    {
    	otherBubbles.Remove(idx);
    }
	}
